<template>
	<view class="login-container">
		<uni-forms ref="loginForm" :model="form">
			<uni-forms-item name="loginName">
				<uni-easyinput v-model="form.loginName" placeholder="用户名" />
			</uni-forms-item>
			<uni-forms-item name="password">
				<uni-easyinput type="password" v-model="form.password" placeholder="密码" />
			</uni-forms-item>
			
			<uni-forms-item>
				<uni-data-checkbox
				    v-model="rememberMe"
				    multiple
				    :localdata="[{ text: '记住我', value: 'yes' }]"
				    style="font-size: 14px;"
				/>
				<!-- <label style="font-size: 14px;">
					<input type="checkbox" v-model="rememberMe" style="transform: scale(0.8);" />
					记住密码
				</label>
				<checkbox-group>
					<label>
						<checkbox value="cb" checked="true" />选中
					</label>
					<label>
						<checkbox value="cb" />未选中
					</label>
				</checkbox-group> -->
			</uni-forms-item>
			<button type="primary" @click="submitForm">登录</button>

		</uni-forms>
	</view>
</template>

<script>
import { loginApi } from '@/api';
	export default {
		data() {
			return {
				form: {
					loginName: '',
					password: ''
				},
				rememberMe: []
			};
		},
		beforeMount() {
			const user = uni.getStorageSync('user')
			console.log("user :>> ", user)
			if (user) {
				uni.redirectTo({
					url: user.role == 0 ? '/pages/admin/index' : '/pages/agent/index'
				});
			}

			const saved = uni.getStorageSync('saved_login')
			console.log("saved :>> ", saved)
			if (saved) {
				this.form.loginName = saved.loginName
				this.form.password = saved.password
				this.rememberMe = ['yes']
			}
		},
		methods: {
			async submitForm() {
				try {
					const result = await this.$refs.loginForm.validate();
					// 账号正则：只能包含字母、数字，长度为 3 - 20 位
					const usernameRegex = /^[a-zA-Z0-9]{3,20}$/;
					// 密码正则：长度为 6 - 20 位，至少包含一个字母和一个数字
					const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,20}$/;
					if (!result.loginName) {
						uni.showToast({
							title: '请输入账号',
							icon: 'none'
						});
					}
					// else if (!usernameRegex.test(result.loginName)) {
					// 	uni.showToast({
					// 		title: '账号格式错误，只能包含字母和数字，长度为 3 - 20 位',
					// 		icon: 'none'
					// 	});
					// }
					else if (!result.password) {
						uni.showToast({
							title: '请输入密码',
							icon: 'none'
						});
					}
					// else if (!passwordRegex.test(result.password)) {
					// 	uni.showToast({
					// 		title: '密码格式错误，长度为 6 - 20 位，至少包含一个字母和一个数字',
					// 		icon: 'none'
					// 	});
					// }
					else {
						try {
							uni.showLoading({
								title: '登录中',
								mask: true
							});
							const {data} = await loginApi(this.form)
							// console.log(data)
							uni.setStorageSync('token', data.authorization);
							uni.setStorageSync('user', data)
							uni.hideLoading()
							// uni.navigateTo({
							// 	url: '/pages/admin/index'
							// });

							console.log("this.rememberMe ",this.rememberMe)
							if (this.rememberMe.length > 0) {
								console.log("记录", {
									loginName: this.form.loginName,
									password: this.form.password
								})
								uni.setStorageSync('saved_login', {
									loginName: this.form.loginName,
									password: this.form.password
								})
							} else {
								console.log("清空")
								uni.removeStorageSync('saved_login')
							}
							
							if (data.role == 0) {
								uni.navigateTo({
									url: '/pages/admin/index'
								});
							} else {
								uni.navigateTo({
									url: '/pages/agent/index'
								});
							}


							
						} catch (error) {
							
						}
						

						// await uni.request({
						// 	url: `${this.$config.API_BASE_URL}/user/login`,
						// 	method: 'POST',
						// 	data: this.form,
						// 	success: (res) => {
						// 		if (res.data.code == 200) {
						// 			if (res.data.data.role == 0) {
						// 				uni.navigateTo({
						// 					url: '/pages/admin/index'
						// 				});
						// 			} else {
						// 				uni.navigateTo({
						// 					url: '/pages/agent/index'
						// 				});
						// 			}
						// 		} else {
						// 			console.log(res)
						// 			uni.showToast({
						// 				title: res.data.message,
						// 				icon: 'none'
						// 			});
						// 		}

						// 	}
						// });
					}
				} catch (e) {
					uni.showToast({
						title: e.message,
						icon: 'none'
					});
				}
			}
		}
	
	}
</script>

<style lang="scss">
.login-container {
		padding: 20px;
	}
</style>
